package it.orma.iridehr.config;

import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.format.Formatter;

public class DateFormatter implements Formatter<Date> {
	@Override
	public String print(Date date, Locale locale) {
		if (date == null) {
			return "";
		}

		return DateFormatUtils.format(date,"dd/MM/yyyy",locale);
	}

	@Override
	public Date parse(String text, Locale locale) throws ParseException {
		if (StringUtils.isEmpty(text)) {
			return null;
		}
		return DateUtils.parseDate(text, new String[]{"dd/MM/yyyy"});
	}
}
